package com.itheima.mapper;

import com.itheima.entity.AddressBook;

import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 地址持久层接口
 * @author msi
 */
@Mapper
public interface AddressMapper {
    /**
     * 获取地址列表
     * @return 返回地址
     */
    @Select("select id, user_id, consignee, sex, phone, province_code," +
            " province_name, city_code, city_name, district_code, district_name, " +
            "detail, label, is_default, create_time, update_time, create_user, update_user," +
            " is_deleted from reggie.address_book")
    List<AddressBook> getList();

    /**
     * 新增地址
     * @param addressBook 地址对象
     * @return 改变行数
     */
    @Insert("insert into reggie.address_book (id, user_id, consignee, sex, phone, detail, label, create_time, update_time, create_user,update_user) " +
            "values (#{id},#{userId},#{consignee},#{sex},#{phone},#{detail},#{label},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    int insertAddress(AddressBook addressBook);

    /**
     * 修改地址
     * @param addressBook  地址对象
     * @return 改变行数
     */
    @Update("update reggie.address_book set consignee=#{consignee}," +
            "phone=#{phone},sex=#{sex},detail=#{detail},label=#{label},is_default=#{isDefault},is_deleted=#{isDeleted}" +
            ",update_time=#{updateTime},update_user=#{updateUser} where id= #{id}")
    int update(AddressBook addressBook);

    /**
     * 获取一条地址
     * @param id 编号
     * @return 地址对象
     */
    @Select("select id, user_id, consignee, sex," +
            " phone, province_code, province_name," +
            " city_code, city_name, district_code, district_name, " +
            "detail, label, is_default, create_time, update_time, create_user, " +
            "update_user, is_deleted from reggie.address_book where id = #{id}")
    AddressBook getOneAddress(String id);

    /**
     * 删除地址
     * @param ids 地址
     * @return
     */
    @Delete("delete from reggie.address_book where id = #{id}")
    int delete(String ids);

    /**
     * 获取默认地址
     * @return 返回地址对象
     */
    @Select("select id, user_id, consignee, sex, phone, province_code, province_name," +
            " city_code, city_name, district_code, district_name, detail, label, is_default," +
            " create_time, update_time, create_user, update_user, is_deleted from reggie.address_book where is_default=1")
    AddressBook getDefaultAddress();

    /**
     * 修改地址为默认地址
     * @param id id
     * @return 返回更改行数
     */
    @Update("update reggie.address_book set is_default=1 where id=#{id}")
    int updateDefault(String id);

    /**
     * 修改默认地址为普通
     * @param id id
     */
    @Update("update reggie.address_book set is_default=0 where id=#{id}")
    void updateDefaultFalse(String id);
}
